﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Tezzanator.Net.XNAGUI;
using Tezzanator.Net.XNAGUI.Controls;

namespace OpenST.GUI
{
    class AddBugWindow
    {
        Game game;

        private Window DialogWindow;
        public TextButton AddButton;
        private TextButton CloseButton;
        private PictureButton BugPreview;
        public ListBox BugList;


        public AddBugWindow(Game thegame)
        {
            game = thegame;
            Vector2 screensize = new Vector2(game.GraphicsDevice.Viewport.Width, game.GraphicsDevice.Viewport.Height);
            

            // Base window
            DialogWindow = new Window(thegame);
            DialogWindow.Size = new Vector2(332, 190);
            DialogWindow.Position = (screensize / 2) - (DialogWindow.Size / 2) - new Vector2(0, 64);
            DialogWindow.TitleFont = game.Content.Load<SpriteFont>("Fonts/WindowTitle");
            DialogWindow.Title = "Add a new bug...";
            DialogWindow.Visible = false;
            OpenST.GetComponent<Gui>(game).RootControl.AddChild(DialogWindow);

            // Add Button
            AddButton = new TextButton("Add", DialogWindow.TitleFont, DialogWindow.BlankPixel);
            AddButton.BackgroundColorModifier = new Color(20, 20, 20, 180);
            AddButton.Size = new Vector2(65, 25);
            AddButton.Position = new Vector2(254, 36 + 64 + 12);
            DialogWindow.AddChild(AddButton);

            // Close Button
            CloseButton = new TextButton("Close", DialogWindow.TitleFont, DialogWindow.BlankPixel);
            CloseButton.BackgroundColorModifier = new Color(20, 20, 20, 180);
            CloseButton.Size = new Vector2(65, 25);
            CloseButton.Position = new Vector2(254, 36 + 64 + 25 + 24);
            CloseButton.OnClick += new BaseControl.ControlEventHandler(CloseButton_OnClick);
            DialogWindow.AddChild(CloseButton);

            BugList = new ListBox(game);
            BugList.Size = new Vector2(230, 64 + 25 + 24 + 25);
            BugList.Position = new Vector2(12, 36);
            BugList.ItemFont = game.Content.Load<SpriteFont>("Fonts/WindowTitle");
            BugList.OnSelectedItemChanged += new BaseControl.ControlEventHandler(BugList_OnSelectedItemChanged);
            DialogWindow.AddChild(BugList);

            DataFormats.BugList allbugs = game.Content.Load<DataFormats.BugList>("Bugs/BugList");
            //Addbugwindow.Show();

            foreach (string s in allbugs.List)
            {
                DataFormats.BugData Bugdata = game.Content.Load<DataFormats.BugData>(s);
                this.BugList.List.Add(Bugdata.Name, s);
            }

            BugPreview = new PictureButton(DialogWindow.BlankPixel);
            BugPreview.Size = new Vector2(64, 64);
            BugPreview.Position = new Vector2(254, 36);
            DialogWindow.AddChild(BugPreview);
        }

        void BugList_OnSelectedItemChanged(BaseControl Sender)
        {
            // Very messy code
            // Basically I don't know how to access a dictionary by index...
            // So I copy the values I need to an array and use that...
            // TODO: Find out how to index dictionary
            ListBox LB = (ListBox)Sender;
            List<string> stringlist = new List<string>();
            foreach (KeyValuePair<string, object> item in LB.List)
            {
                stringlist.Add((string)item.Value);
            }

            DataFormats.BugData bd = game.Content.Load<DataFormats.BugData>(stringlist[LB.SelectedIndex]);

            BugPreview.Picture = game.Content.Load<Texture2D>(bd.MenuSprite);
            //throw new NotImplementedException();
        }

        void CloseButton_OnClick(BaseControl Sender)
        {
            DialogWindow.Visible = false;
        }

        public void Show()
        {
            DialogWindow.Visible = true;
        }

        public void Hide()
        {
            DialogWindow.Visible = false;
        }
    }
}
